/* vim:ft=objc:
 * $Id: DetachList.m 118 2005-05-14 04:31:00Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#import "DetachList.h"
#import "AccountInfo.h"
#import <MacBiffPlugIn/PreferencesController.h>
#import <MacBiffPlugIn/Account.h>
#import <MacBiffPlugIn/Folder.h>
#import <MacBiffPlugIn/debug.h>

@implementation DetachList

- (id) init
{
	if ( ( self = [super init] ) != Nil ) {
		acctList = Nil;
		unreadOnly = YES;
		[unreadOnlyButton setState: [[[prefs display]
			objectForKey: @"Display State"] intValue]];
	}
	return self;
}


- (void) dealloc
{
	if ( acctList != Nil ) [acctList release];
	[super dealloc];
}


- (void) expand
{
	NSEnumerator *itemEnum = [acctList objectEnumerator];
	Account* item = Nil;
	int expand = [expandButton state];

	while ( (item = [[itemEnum nextObject] account]) ) {
		if ( expand == NSOnState ) {
			/* BUG IN NSOutlineView! */
			[folderOutlineView collapseItem: item
				       collapseChildren: YES];
			[folderOutlineView expandItem: item
				       expandChildren: YES];
		} else if ( expand == NSOffState ) {
			[folderOutlineView collapseItem: item
				       collapseChildren: YES];
		}
	}
}


- (IBAction)showOnlyUnread:(id)sender
{
	/* Make the OV reflect the new state */
	unreadOnly = ([unreadOnlyButton state] == NSOnState);
	[folderOutlineView reloadData];
	[self expand];
}

- (void) setAccountList: (NSArray*) list
{
	if ( acctList != Nil ) {
		[acctList release];
	}
	acctList = list;
	[acctList retain];
	[folderOutlineView reloadData];
	[self expand];
}


- (void) update: (id) obj
{
	[folderOutlineView reloadData];
	[self expand];
}


- (IBAction) expandAll: (id) sender
{

	[[prefs display] setObject:
			[NSNumber numberWithInt: [expandButton state]]
		forKey: @"Display Prefs"];

	[self expand];
}


- (void) detach
{
	[folderOutlineView reloadData];
	[self expand];
	[window makeKeyAndOrderFront: self];
	[NSApp activateIgnoringOtherApps: YES];
}


- (NSArray*) childArrayForItem: (id) item
{
	Account *acct = (Account*)item;
	Folder *folder = (Folder*)item;
	NSArray *olist = Nil;
	NSMutableArray *list = Nil;

	if ( [item isKindOfClass: [Account class]] ) {
		olist = [acct allFolders];
	} else if ( [item isKindOfClass: [Folder class]] ) {
		olist = [folder subFolders];
	} else {
		alert("Recivied item of unknown class.\n");
		return Nil;
	}

	if ( unreadOnly ) {
		list = [[[NSMutableArray alloc] init] autorelease];
		Folder *fol;
		NSEnumerator *folEnum = [olist objectEnumerator];
		while ( (fol = [folEnum nextObject]) ) {
			if ( [fol wantsShown] ) {
				[list addObject: fol];
			}
		}
		return list;
	} else {
		return olist;
	}
}


- (int) outlineView: (NSOutlineView*) ov
	numberOfChildrenOfItem: (id) item
{
	NSArray *arr = Nil;
	if ( !acctList ) return 0;
	if ( !item ) return [acctList count];

	arr = [self childArrayForItem: item];
	if ( !arr ) return 0;

	return [arr count];
}


- (BOOL) outlineView: (NSOutlineView*) ov
	isItemExpandable: (id) item
{
	return ([self outlineView: ov numberOfChildrenOfItem: item] > 0);
}


- (id) outlineView: (NSOutlineView*) ov
	child: (int) idx
	ofItem: (id) item
{
	if ( !acctList ) return nil;
	if ( !item )
		return [[acctList objectAtIndex: idx] account];

	return [[self childArrayForItem: item] objectAtIndex: idx];

	return nil;
}


- (id) outlineView: (NSOutlineView*) ov
	objectValueForTableColumn: (NSTableColumn*) tableColumn
	byItem: (id) item
{
	if ( !acctList || !item ) return nil;
	NSString *ident = [tableColumn identifier];
	Account *acct = (Account*) item;
	Folder *fol = (Folder*) item;

	if ( [ident isEqual: @"name"] ) {
		return [item name];
	} else if ( [ident isEqual: @"unread"] ) {
		if ( [item isKindOfClass: [Account class]] ) {
			return [NSNumber numberWithInt: [acct totalUnread]];
		} else if ( [item isKindOfClass: [Folder class]] ) {
			if ( [fol canContainMail] )
				return [NSNumber numberWithInt: [fol unreadMail]];
			else
				return @"-";
		}
	} else if ( [ident isEqual: @"total"] ) {
		if ( [item isKindOfClass: [Account class]] ) {
			return [NSNumber numberWithInt: [acct total]];
		} else if ( [item isKindOfClass: [Folder class]] ) {
			if ( [fol canContainMail] )
				return [NSNumber numberWithInt: [fol totalMail]];
			else
				return @"-";
		}
	}

	return Nil;
}


- (void) outlineView: (NSOutlineView *) ov
	setObjectValue: (id) object
	forTableColumn: (NSTableColumn*) tableColumn
	byItem: (id) item
{
	/* We don't allow editing here */
	return;
}


@end
